package cn.south.campus.admin.sys.service.impl;

import cn.south.campus.admin.sys.mapper.SysRoleMenuMapper;
import cn.south.campus.admin.sys.model.entity.SysRoleMenu;
import cn.south.campus.admin.sys.service.SysRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 实现层
 *
 * @author  chow
 * @since  2024-11-15
 **/
@Service
@AllArgsConstructor
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    private final SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRelation(Long roleId, Set<Long> menuIds) {
        List<SysRoleMenu> roleMenuList = menuIds.stream().map(menuId -> new SysRoleMenu(roleId, menuId)).collect(Collectors.toList());
        saveBatch(roleMenuList);
    }

    @Override
    public void delRelation(Long roleId) {
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().lambda().eq(SysRoleMenu::getRoleId, roleId));
    }

    @Override
    public List<Long> findRelation(Long roleId) {
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().lambda().eq(SysRoleMenu::getRoleId, roleId));
        return sysRoleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
    }
}
